package com.woniu.permissions.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.permissions.dto.MenuDto;
import com.woniu.permissions.mapper.TMenuMapper;
import com.woniu.permissions.model.TMenu;
import com.woniu.permissions.service.TMenuService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年06月14日
 */
@Service
public class TMenuServiceImpl extends ServiceImpl<TMenuMapper, TMenu> implements TMenuService {


    @Resource
    private TMenuMapper tMenuMapper;


    @Override
    public List<MenuDto> selectMenu(Long l) {
        List<TMenu> list=null;
        list = tMenuMapper.selectMenus(l);


        ArrayList<MenuDto> data = new ArrayList<>();


        for (TMenu menu : list) {
            if (menu.getMenuId().longValue() == 0) {
                MenuDto level1Menu = BeanUtil.toBean(menu, MenuDto.class); //方法作用：将 TResource转换成dto
                level1Menu.setParentId(menu.getMenuId());
                level1Menu.setName(menu.getMenuName());
                data.add(level1Menu);
            }
        }

        System.out.println(data);
        for (TMenu menu1 : list) {
            if (menu1.getMenuId().longValue() != 0) {
                MenuDto level2Menu = BeanUtil.toBean(menu1, MenuDto.class);//方法作用：将 TResource转换成dto
                level2Menu.setParentId(menu1.getMenuId());
                level2Menu.setName(menu1.getMenuName());
                for (MenuDto levelone : data) {
                    if (levelone.getId().longValue() == menu1.getMenuId().longValue()) {

                        levelone.getChildren().add(level2Menu);
                    }
                }
            }
        }

        System.out.println(list);
        return data;
    }
}
